﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_PersonPrepareForm
 * 类型：       控制层
 * 功能说明：   报到准备页面
 * 作者：       ly
 * 创建日期：   2008/3/14
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using Shpm.System.Validator;
using log4net;

public partial class App_Input_PersonPrepareForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //页面参数传递
            if (Context.Items.Contains("PERSON_ID"))
            {
                ViewState["PERSON_ID"] = Context.Items["PERSON_ID"];
            }
            else
            {
                ViewState["PERSON_ID"] = null;
            }
            if (Context.Items["EMPLOYEE_ID"] != null)
            {
                ViewState["EMPLOYEE_ID"] = Context.Items["EMPLOYEE_ID"];
            }
            else
            {
                ViewState["EMPLOYEE_ID"] = null;
            }
            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["ParamPageIndex"] = Convert.ToString(Context.Items["ParamPageIndex"]);
            }
            // 接收传递参数为父页编号
            if (Context.Items.Contains("URL"))
            {
                ViewState["URL"] = Convert.ToString(Context.Items["URL"]);
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["CMB_GROUP_ID"] = Context.Items["CMB_GROUP_ID"];
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["CMB_JOB_ID"] = Context.Items["CMB_JOB_ID"];
            }
            if (Context.Items.Contains("EMPLOYEE_STATUS"))
            {
                this.ViewState["EMPLOYEE_STATUS"] = Context.Items["EMPLOYEE_STATUS"];
            }

            ViewState["STATE"] = "INS";
        }

        PagePreRender();
    }

    #region 设置标签
    protected void lnkTab_Command(object sender, CommandEventArgs e)
    {
        string directUrl = "~/App/Input/SaleProcesseMembershipForm.aspx";
        switch (e.CommandName)
        {
            case "btn0":
                // 基本信息
                directUrl = "~/App/Input/PersonInfoForm.aspx";
                break;
            case "btn1":
                // 学历

                directUrl = "~/App/Input/PersonInfoEdu.aspx";
                break;
            case "btn3":
                // 工作经历
                directUrl = "~/App/Input/PersonInfoWork.aspx";
                break;
            case "btn2":
                // 家庭情况
                directUrl = "~/App/Input/PersonInfoFamily.aspx";
                break;
            case "btn4":
                // 福利
                directUrl = "~/App/Input/PersonInfoWelfare.aspx";
                break;
            case "btn5":
                //薪酬
                directUrl = "~/App/Input/PersonInfoSalaryForm.aspx";
                break;
            case "btn6":
                //奖惩
                directUrl = "~/App/Input/PersonBonusPenaltyForm.aspx";
                break;
            case "btn7":
                //报道
                directUrl = "~/App/Input/PersonPrepareForm.aspx";
                break;
            case "btn8":
                //培训
                directUrl = "~/App/Input/PersonTrainForm.aspx";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["PERSON_ID"])))
        {
            Context.Items["PERSON_ID"] = this.ViewState["PERSON_ID"];
            Context.Items["EMPLOYEE_ID"] = this.ViewState["EMPLOYEE_ID"];
            Context.Items["CMB_JOB_ID"] = ViewState["CMB_JOB_ID"];
            Context.Items["CMB_GROUP_ID"] = ViewState["CMB_GROUP_ID"];
            Context.Items["CMB_STATUS_ID"] = ViewState["CMB_STATUS_ID"];
            Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
            Context.Items["URL"] = ViewState["URL"];
            Context.Items["EMPLOYEE_STATUS"] = ViewState["EMPLOYEE_STATUS"];
            ControllerUtils.Transfer(this.Context, directUrl, false);
        }
        else
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0035").ToString();
        }
    }
    #endregion

    //数据加载处理
    public void PagePreRender()
    {
        PersonEmployeeDao dao = new PersonEmployeeDao();
        PersonEmployeeBean bean = new PersonEmployeeBean();
        PersonPrepareBean prepareBean = new PersonPrepareBean();
        PersonPrepareDao prepareDao = new PersonPrepareDao();
        CommonDictionaryBean commonBean = new CommonDictionaryBean();
        IList commonlist = new ArrayList();
        IList list = new ArrayList();

        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["EMPLOYEE_ID"])))
        {
            bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            bean = dao.Find(bean);

            this.personName.Text = bean.EmployeeName;
            this.personJob.Text = bean.JobName;
            this.personDepart.Text = bean.GroupName;
            this.arriveDate.Text = Convert.ToDateTime(bean.EmployeeDate).ToShortDateString();

            commonBean.Catalog = "T_INTERVIEW_PREPARE.PREPARE_TYPE";
            commonlist = prepareDao.FindByCatalog(commonBean);

            prepareBean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            list = prepareDao.getInfoList(prepareBean);

            if (list.Count == 0)
            {
                int k = 0;

                for (int i = 0; i < commonlist.Count; i++)
                {
                    commonBean = (CommonDictionaryBean)commonlist[i];
                    setLayout(commonBean, k);
                    k++;
                }
            }
            else
            {
                int k = 0;

                for (int i = 0; i < commonlist.Count; i++)
                {
                    commonBean = (CommonDictionaryBean)commonlist[i];
                    setLayout(commonBean, k);
                    k++;
                }
                for (int j = 0; j < list.Count; j++)
                {
                    prepareBean = (PersonPrepareBean)list[j];
                    compare(prepareBean, commonlist.Count);
                }
            }
        }
    }

    //动态加载画面
    public void setLayout(CommonDictionaryBean commonBean, int k)
    {
        TableRow row = new TableRow();

        TableCell cell;

        //编号
        cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Center;
        LiteralControl tt = new LiteralControl(commonBean.DictNo);
        tt.ID = string.Format("tt_{0}", k);
        cell.Controls.Add(tt);
        row.Cells.Add(cell);

        //准备工作
        cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Left;
        cell.Controls.Add(new LiteralControl(commonBean.Content));
        row.Cells.Add(cell);

        //负责人
        cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Left;
        TextBox box;
        box = new TextBox();
        box.ID = string.Format("TextItemId_{0}", k);
        box.Width = 60;
        box.Attributes.Add("onpropertychange", "textChange('"+ k +"')");
        cell.Controls.Add(box);
        box = new TextBox();
        box.ID = string.Format("NameItemId_{0}", k);
        box.Width = 70;
        box.BackColor = System.Drawing.Color.Silver;
        //box.ReadOnly = true;
        ImageButton button = new ImageButton();
        button.ID = string.Format("ButtonItemId_{0}", k);
        button.ImageUrl = "../../images/search.jpg";
        button.Attributes.Add("onclick", "popUp('" + k + "')");
        cell.Controls.Add(box);
        cell.Controls.Add(button);
        row.Controls.Add(cell);

        //选择
        cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Center;
        CheckBox Checkbox;
        Checkbox = new CheckBox();
        Checkbox.ID = string.Format("SelectItemId_{0}", k);
        cell.Controls.Add(Checkbox);
        row.Controls.Add(cell);

        //完成
        cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Center;
        Checkbox = new CheckBox();
        Checkbox.ID = string.Format("FinishItemId_{0}", k);
        Checkbox.Attributes.Add("onclick", "Change('" + k + "')");
        cell.Controls.Add(Checkbox);
        row.Controls.Add(cell);

        tbPrepare.Controls.Add(row);
    }

    //数据比较加载
    public void compare(PersonPrepareBean bean, int count)
    {
        for (int i = 1; i <= count; i++)
        {
            LiteralControl tt = new LiteralControl();
            tt = (LiteralControl)tbPrepare.Rows[i].Cells[0].FindControl(string.Format("tt_{0}", i - 1));
            int t = Convert.ToInt32(tt.Text);
            if (Convert.ToInt32(bean.PrepareType) == t)
            {
                CheckBox checkBox = (CheckBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("SelectItemId_{0}", i - 1));
                checkBox.Checked = bean.ReadFlg == 1;

                TextBox textBox = (TextBox)tbPrepare.Rows[i].Cells[2].FindControl(string.Format("TextItemId_{0}", i - 1));
                textBox.Text = Convert.ToString(bean.PrepareEmployeeId);

                TextBox textBox1 = (TextBox)tbPrepare.Rows[i].Cells[2].FindControl(string.Format("NameItemId_{0}", i - 1));
                textBox1.Text = Convert.ToString(bean.EmployeeName);

                CheckBox checkBox1 = (CheckBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("FinishItemId_{0}", i - 1));
                checkBox1.Checked = bean.FinishFlg == 1;
            }
        }
    }

    //数据保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        PersonPrepareBean bean;
        PersonPrepareDao dao = new PersonPrepareDao();
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        IList list = new ArrayList();

        for (int i = 1; i <= this.tbPrepare.Rows.Count - 1; i++)
        {
            LiteralControl tt = new LiteralControl();
            tt = (LiteralControl)tbPrepare.Rows[i].Cells[0].FindControl(string.Format("tt_{0}", i - 1));
            TextBox textBox = (TextBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("TextItemId_{0}", i - 1));
            TextBox textBox1 = (TextBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("NameItemId_{0}", i - 1));
            CheckBox box = (CheckBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("SelectItemId_{0}", i - 1));
            CheckBox box1 = (CheckBox)tbPrepare.Rows[i].Cells[4].FindControl(string.Format("FinishItemId_{0}", i - 1));

            if(!string.IsNullOrEmpty(textBox.Text.Trim()))
            {
                //数据验证通过
                if (checkNo(i))
                {
                    bean = new PersonPrepareBean();
                    bean.PrepareType = Convert.ToString(tt.Text);
                    bean.PrepareEmployeeId = string.IsNullOrEmpty(textBox.Text.Trim()) ? string.Empty : textBox.Text.Trim();
                    bean.ReadFlg = (byte)(box.Checked ? 1 : 0);
                    bean.FinishFlg = (byte)(box1.Checked ? 1 : 0);
                    bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
                    bean.CancelFlg = 0;
                    bean.UserId = userId;
                    list.Add(bean);
                }
                //没通过
                else
                    goto resetLayout;
            }
        }

        bean = new PersonPrepareBean();
        bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        dao.deleteInfo(bean);
        dao.insertInfoList(list);
        senMessage(list);
        
        //提示保存成功
        Response.Write("<script language=JavaScript>" +
                "alert('" + HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0037").ToString().Replace("{0}", "数据保存") + "');</script>");
        
        //重置画面
resetLayout:
        while (tbPrepare.Rows.Count > 1)
        {
            tbPrepare.Rows.RemoveAt(1);
        }

        PagePreRender();
    }

    //数据验证
    //public Boolean CheckData(int i)
    //{
    //    ComValidateChecker validator = new ComValidateChecker();

    //    TextBox box = (TextBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("TextItemId_{0}", i - 1));
    //    CheckBox checkBox = (CheckBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("SelectItemId_{0}", i - 1));
    //    if (checkBox.Checked)
    //    {
    //        validator.Add(new CheckRequired(box.Text, new ComMessage("E0094", "第" + i + "行，负责人")));
    //        if (validator.DoValidator() == false)
    //        {
    //            输出校验失败信息
    //           ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
    //            return false;
    //        }
    //        if (checkNo(i) == false)
    //        {
    //            return false;
    //        }
    //    }

    //    return true;
    //}

    //负责人验证
    public Boolean checkNo(int i)
    {
        EmployeeDao dao = new EmployeeDao();
        IList lstRst = null;
        EmployeeBean bean = new EmployeeBean();
        TextBox box = (TextBox)tbPrepare.Rows[i].Cells[3].FindControl(string.Format("TextItemId_{0}", i - 1));
        if (!string.IsNullOrEmpty(box.Text.Trim()))
        {
            bean.EmployeeId = Convert.ToString(box.Text.Trim());
            lstRst = dao.Load("", "", "", bean.EmployeeId);
        }
        if (lstRst.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}",box.Text.Trim());
            return false;
        }
        else
            return true;
    }

    //发送信息事务处理
    public void senMessage(IList list)
    {
        PersonPrepareBean bean = new PersonPrepareBean();
        PersonPrepareDao dao = new PersonPrepareDao();

        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["EMPLOYEE_ID"])))
        {
            bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            bean = dao.getName(bean);
            string name = bean.EmployeeName;

            for (int i = 0; i < list.Count; i++)
            {
                bean = (PersonPrepareBean)list[i];
                bean = dao.getPrepareInfo(bean);

                if (bean.ReadFlg == 1)
                {
                    message(":" + name + "的到岗准备工作确认:"+ bean.PrepareTypeName +"！", bean.PrepareEmployeeId);
                }
            }
        }
    }

    //发信
    public void message(string msg, string employeeId)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

        MessageBean beanMsg = new MessageBean();
        beanMsg.Subject = "新员工到岗准备工作确认" + DateUtils.convertToString(DateTime.Now);
        beanMsg.Message = DateUtils.convertToString(DateTime.Now) + msg;
        beanMsg.MessageType = 3;
        beanMsg.SendDate = DateTime.Now;
        beanMsg.ReplyFlag = false;
        beanMsg.ReaderHistoryFlag = false;
        beanMsg.ImportantFlag = false;
        beanMsg.EmployeeId = emplyId;
        beanMsg.UserId = loginId;
        CommonSendMessageUtils sendMsg = new CommonSendMessageUtils(beanMsg, employeeId);
        sendMsg.SendMessage();
    }

    //返回
    protected void btnBack_Click(object sender, EventArgs e)
    {
        // 传递分页编号
        Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["CMB_GROUP_ID"]);
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["CMB_JOB_ID"]);
        //string url = Convert.ToString(ViewState["URL"]);
        string url = "~/App/Input/PersonEmployeeSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);
    }
}
